package tech.bmatch.meituan;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.bmatch.meituan.model.Dish;
import tech.bmatch.meituan.model.Merchant;
import tech.bmatch.meituan.model.MerchantSearchParam;
import tech.bmatch.meituan.service.impl.MerchantServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @program: S1P1FinalProject
 * @description: 主程序
 * @author: Awake
 * @create: 2018-09-28 17:15
 **/
public class Application {
    //Logger
    public static final Logger logger = LoggerFactory.getLogger(Application.class);

    //静态创建的服务
    private static MerchantServiceImpl merchantService = new MerchantServiceImpl();

    public static void main(String[] args) {
        logger.info("Start application.");
        merchantService.init();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String command = null;
            try {
                command = bufferedReader.readLine();
            } catch (IOException e) {
                logger.error("", e);
            }

            System.out.println("执行命令" + command);
            if (command.startsWith("addMerchant ")) {
                String content = command.replaceAll("addMerchant ", "");
                initMerchant(content.split(" "));
            }

            if (command.startsWith("search ")) {
                String content = command.replaceAll("search ", "");
                search(content);
            }

            if (command.startsWith("addDish ")) {
                String content = command.replaceAll("addDish ", "");
                addDish(content.split(" "));
            }

            if (command.startsWith("displayDishes ")) {
                String id = command.replaceAll("displayDishes ", "");
                displayDishes(id);
            }
        }
    }

    /**
     * 添加一个商家
     *
     * @param items 传入参数
     */
    public static void initMerchant(String[] items) {
        Merchant merchant = new Merchant();
        merchant.setId(items[0]);
        merchant.setName(items[1]);
        merchant.setLon(Double.valueOf(items[2]));
        merchant.setLat(Double.valueOf(items[3]));

        merchantService.addMerchant(merchant);
        merchantService.storeMerchants();
    }

    /**
     * 搜索商家
     *
     * @param name 查询名字
     */
    private static void search(String name) {
        MerchantSearchParam param = new MerchantSearchParam();
        param.setName(name);
        param.setLon(120.026208);
        param.setLat(30.279212);
        //得到商家列表，通过距离计算
        List<Merchant> merchantList = merchantService.search(param);
        for (Merchant merchant : merchantList) {
            System.out.println("商家名称：" + merchant.getName());
            Double distance = merchant.getDistance();
            System.out.println("距离：" + distance.intValue() + "m");
            System.out.println("-------------------");
        }
    }

    /**
     * 添加一种菜品到指定商家
     *
     * @param items
     */
    private static void addDish(String[] items) {
        String merchantId = items[0];
        Dish dish = new Dish();
        dish.setName(items[1]);
        dish.setPrice(Double.valueOf(items[2]));

        merchantService.addDish(dish, merchantId);
        merchantService.storeMerchants();
    }

    /**
     * 根据商家id浏览菜品
     *
     * @param id 商家id
     */
    private static void displayDishes(String id) {
        List<Dish> dishes = merchantService.findDishesByMerchantId(id);
        dishes.forEach(dish -> {
            System.out.println("-------------------");
            System.out.println("菜品名称：" + dish.getName());
            System.out.println("菜品价格：" + dish.getPrice());
        });
    }
}

